package com.erik.lin.service;

import com.erik.lin.orm.dao.ApiDao;
import com.erik.lin.orm.po.ApiPo;
import com.erik.lin.util.AppContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by sys on 2017/3/31.
 */
@Service
public class ApiService {

    @Autowired
    private ApiDao apiDao;

    public Map<String, Object> removeAll() {
        Map<String, Object> rs = new HashMap<>();
        rs.put("success", true);
        try{
            EhCacheCacheManager cacheManager = (EhCacheCacheManager) AppContext.getBean("linCacheManager");
            Cache apiCache = cacheManager.getCache("api");
            apiCache.clear();
        }catch (Exception e) {
            rs.put("success", false);
            rs.put("msg", e.getMessage());
            e.printStackTrace();
        }finally {
            return rs;
        }
    }

    public ApiPo getApiById(String id) {
        return apiDao.getApiById(id);
    }

    public List<ApiPo> getApisByApiLabel(String apiLabel) {
        return apiDao.getApisByApiLabel("%"+apiLabel+"%");
    }

    public void save(ApiPo po) {
        apiDao.save(po);
    }

    public void update(ApiPo po) {
        apiDao.update(po);
    }

    public void deleteById(String id) {
        apiDao.deleteById(id);
    }
}
